<template>
  <div class="w-full h-full">
    <div class="common-code" ref="code"></div>
  </div>
</template>
<script lang="ts" setup>
import * as monaco from "monaco-editor/esm/vs/editor/editor.api";
import "monaco-editor/esm/vs/basic-languages/javascript/javascript.contribution";
import "monaco-editor/esm/vs/basic-languages/html/html.contribution";
import "monaco-editor/esm/vs/basic-languages/css/css.contribution";
import {
  ref,
  watch,
  withDefaults,
  defineProps,
  defineExpose,
  defineEmits,
} from "vue";
import { onMounted } from "@vue/runtime-core";
import { IStandaloneCodeEditor } from "monaco-editor";

let monacoEditor: IStandaloneCodeEditor = null;

const code = ref<null | HTMLDivElement>(null);

const props = withDefaults(
  defineProps<{
    value: string;
    language: string;
  }>(),
  {
    language: "javascript",
  }
);
const emits = defineEmits(["update:value"]);
watch(
  () => props.value,
  (nv) => {
    if (monacoEditor) {
      monacoEditor.setValue(nv);
    }
  }
);
onMounted(() => {
  console.log( props.value)
  monacoEditor = monaco.editor.create(code.value as HTMLDivElement, {
    theme: "vs-dark", // 主题
    value: props.value, // 默认显示的值
    language: props.language,
    fontSize: 18,
    folding: true, // 是否折叠
    foldingHighlight: true, // 折叠等高线
    foldingStrategy: "indentation", // 折叠方式  auto | indentation
    showFoldingControls: "always", // 是否一直显示折叠 always | mouseover
    disableLayerHinting: true, // 等宽优化
    emptySelectionClipboard: false, // 空选择剪切板
    selectionClipboard: false, // 选择剪切板
    automaticLayout: true, // 自动布局
    codeLens: true, // 代码镜头
    scrollBeyondLastLine: true, // 滚动完最后一行后再滚动一屏幕
    colorDecorators: true, // 颜色装饰器
    accessibilitySupport: "off", // 辅助功能支持  "auto" | "off" | "on"
    lineNumbers: "on", // 行号 取值： "on" | "off" | "relative" | "interval" | function
    lineNumbersMinChars: 5, // 行号最小字符   number
    enableSplitViewResizing: true,
    readOnly: false, //是否只读  取值 true | false
    minimap: {
      enabled: true, // 不要小地图
    },
  });
});

const confirmHandler = () => {
  emits("update:value", monacoEditor.getValue());
};

defineExpose({
  confirmHandler,
});
</script>

<style lang="scss" scoped>
.common-code {
  height: calc(100% - 50px);
  width: 100%;
}

.tool-btn {
  width: 100%;
  height: 50px;
}
</style>
